package sgi.client;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.JTableHeader;
import javax.swing.table.TableModel;
import javax.swing.table.TableRowSorter;
import javax.swing.JLabel;
import java.awt.Font;
import javax.swing.JTextField;

import net.sourceforge.jdatepicker.impl.JDatePanelImpl;
import net.sourceforge.jdatepicker.impl.JDatePickerImpl;
import net.sourceforge.jdatepicker.impl.UtilDateModel;

import javax.swing.DefaultListModel;
import javax.swing.DefaultRowSorter;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JButton;
import javax.swing.SpringLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.Comparator;
import java.util.Date;
import java.awt.Color;
import java.io.IOException;

import javax.swing.UIManager;
import javax.swing.JTable;

import sgi.client.SGIPurchaseImageController.Source;
import sgi.details_new.ImageDetails;
import sgi.misc.FloatDimension;

public class SGISearchArchiveUI extends SGIClientFrame {

	private JPanel contentPane;
	private static SGISearchArchiveController searchController;
	private JTextField textFieldLocation;
	private DefaultListModel<String> listModel;
	SpringLayout springLayout;
	private SpringLayout sl_datePickerTo;
	private SpringLayout sl_datePickerFrom;
	JDatePickerImpl datePickerTo;
	JDatePickerImpl datePickerFrom;
	private JTable table;
	
	public SGISearchArchiveUI() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 526, 525);
		setTitle("SGI Search - " + ((SGIUserAccountMenuController.create(null)).getUser()).getUsername());
		contentPane = new JPanel();
		contentPane.setLayout(null);
		setContentPane(contentPane);
		
		JLabel lblLocation = new JLabel("Location:");
		lblLocation.setFont(new Font("Tahoma", Font.PLAIN, 13));
		lblLocation.setBounds(10, 26, 62, 14);
		contentPane.add(lblLocation);
		
		textFieldLocation = new JTextField();
		textFieldLocation.setBounds(66, 24, 117, 20);
		contentPane.add(textFieldLocation);
		textFieldLocation.setColumns(10);
		
		UtilDateModel model = new UtilDateModel();
		JDatePanelImpl datePanel = new JDatePanelImpl(model);
		datePickerTo = new JDatePickerImpl(datePanel);
		springLayout = new SpringLayout();
		springLayout.putConstraint(SpringLayout.NORTH, datePickerTo.getJFormattedTextField(), 0, SpringLayout.NORTH, datePickerTo);
		springLayout.putConstraint(SpringLayout.WEST, datePickerTo.getJFormattedTextField(), -90, SpringLayout.EAST, datePickerTo);
		springLayout.putConstraint(SpringLayout.SOUTH, datePickerTo.getJFormattedTextField(), 23, SpringLayout.NORTH, datePickerTo);
		springLayout.putConstraint(SpringLayout.EAST, datePickerTo.getJFormattedTextField(), -35, SpringLayout.EAST, datePickerTo);
		sl_datePickerTo = (SpringLayout) datePickerTo.getLayout();
		datePickerTo.setBounds(388, 23, 109, 31);
		contentPane.add(datePickerTo);
		
		UtilDateModel model1 = new UtilDateModel();
		JDatePanelImpl datePanel1 = new JDatePanelImpl(model1);
		Date date = new Date();
		model1.setDate(((int)date.getYear()+1900), (int)date.getMonth(), (int)date.getDate());
		model1.setSelected(true);
		datePickerFrom = new JDatePickerImpl(datePanel1);
		sl_datePickerTo.putConstraint(SpringLayout.NORTH, datePickerFrom.getJFormattedTextField(), 0, SpringLayout.NORTH, datePickerFrom);
		sl_datePickerTo.putConstraint(SpringLayout.WEST, datePickerFrom.getJFormattedTextField(), -90, SpringLayout.EAST, datePickerFrom);
		sl_datePickerTo.putConstraint(SpringLayout.SOUTH, datePickerFrom.getJFormattedTextField(), 23, SpringLayout.NORTH, datePickerFrom);
		sl_datePickerTo.putConstraint(SpringLayout.EAST, datePickerFrom.getJFormattedTextField(), -35, SpringLayout.EAST, datePickerFrom);
		sl_datePickerFrom = (SpringLayout) datePickerFrom.getLayout();
		datePickerFrom.setBounds(247, 23, 109, 31);
		contentPane.add(datePickerFrom);
		
		JLabel lblFrom = new JLabel("From:");
		lblFrom.setFont(new Font("Tahoma", Font.PLAIN, 13));
		lblFrom.setBounds(209, 26, 42, 14);
		contentPane.add(lblFrom);
		
		JButton btnSearch = new JButton("Search");
		btnSearch.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				Date selectedDateFrom = (Date)datePickerFrom.getModel().getValue();
				Date selectedDateTo = (Date)datePickerTo.getModel().getValue();
				if(selectedDateFrom == null)
					selectedDateFrom = new Date(0,0,1);
				if(selectedDateTo == null)
					selectedDateTo = new Date(199,11,31);
				if(textFieldLocation.getText().equals(""))
					searchController.search((selectedDateFrom.getYear()+1900)+"-"+(selectedDateFrom.getMonth()+1)+"-"+(selectedDateFrom.getDate()),(selectedDateTo.getYear()+1900)+"-"+(selectedDateTo.getMonth()+1)+"-"+(selectedDateTo.getDate()));
				else
					searchController.search((selectedDateFrom.getYear()+1900)+"-"+(selectedDateFrom.getMonth()+1)+"-"+(selectedDateFrom.getDate()),(selectedDateTo.getYear()+1900)+"-"+(selectedDateTo.getMonth()+1)+"-"+(selectedDateTo.getDate()),textFieldLocation.getText());
				
			}
		});
		btnSearch.setBounds(10, 65, 155, 23);
		contentPane.add(btnSearch);
		
		JButton btnPuchaseImage = new JButton("Puchase Image");
		btnPuchaseImage.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				try {
					if(table.getSelectedRow() == -1)
						JOptionPane.showMessageDialog(SGISearchArchiveController.getUI(),"Select image.");
					else
						SGIPurchaseImageController.create(new ImageDetails(Integer.valueOf((String) table.getValueAt(table.getSelectedRow(),0)),(String) table.getValueAt(table.getSelectedRow(),2),(String) table.getValueAt(table.getSelectedRow(),1),new FloatDimension(0,0)),Source.search);
					
				} catch (IOException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			}
		});
		btnPuchaseImage.setBounds(342, 65, 155, 23);
		contentPane.add(btnPuchaseImage);
		
		JButton btnBack = new JButton("Back");
		btnBack.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				SGISearchArchiveController.terminate();
				SGIUserAccountMenuController.create(null);
			}
		});
		btnBack.setBounds(408, 453, 89, 23);
		contentPane.add(btnBack);
		
		JButton btnPurchaseSample = new JButton("Purchase Sample");
		btnPurchaseSample.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				try {
					if(table.getSelectedRow() == -1)
						JOptionPane.showMessageDialog(SGISearchArchiveController.getUI(),"Select image.");
					else
						SGIPurchaseSampleController.create(new ImageDetails(Integer.valueOf((String) table.getValueAt(table.getSelectedRow(),0)),(String) table.getValueAt(table.getSelectedRow(),2),(String) table.getValueAt(table.getSelectedRow(),1),new FloatDimension(0,0)),Source.search);
					
				} catch (IOException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			}
		});
		btnPurchaseSample.setBounds(177, 65, 155, 23);
		contentPane.add(btnPurchaseSample);
		
		JLabel lblTo = new JLabel("To:");
		lblTo.setFont(new Font("Tahoma", Font.PLAIN, 13));
		lblTo.setBounds(366, 27, 29, 14);
		contentPane.add(lblTo);
		
		Object columnNames[] = { "Image ID", "Location", "Date"};
		Object rowData[][] = {};
		DefaultTableModel dtm = new DefaultTableModel(rowData, columnNames) {
		      public boolean isCellEditable(int rowIndex, int mColIndex) {
		          return false;
		        }
		      };
		
		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setBounds(10, 109, 490, 332);
		contentPane.add(scrollPane);
		table = new JTable(dtm);
		table.setBounds(10, 127, 490, 315);
		//table.setAutoCreateRowSorter(true);


		//table.getRowSorter().toggleSortOrder(2);	//makes trouble with row order

	
		JTableHeader header = table.getTableHeader();
		JPanel panel = new JPanel();
		scrollPane.setViewportView(panel);
		panel.setLayout(new BorderLayout());
		panel.add(header, BorderLayout.NORTH);
		panel.add(table, BorderLayout.CENTER);
	}
	
	public void setController(SGISearchArchiveController search)
	{
		SGISearchArchiveUI.searchController = search;
	}
	
	public void addRow (ImageDetails img)
	{
		DefaultTableModel dtm = (DefaultTableModel) table.getModel();
		dtm.insertRow(table.getRowCount(), new Object[] {String.valueOf(img.getImageid()),img.getLocation(),img.getDate()});
		table.setModel(dtm);
	}
	
	public void clearTable()
	{
		DefaultTableModel dtm = (DefaultTableModel) table.getModel();
		while(dtm.getRowCount() > 0) {dtm.removeRow(0);}
	}
}
